const router = require('koa-router')();
const fs = require("fs");
const path = require("path");
router.get('/', async (ctx, next) => {
  console.log(123);
  
  await ctx.render('index', {
    navList:["首页","菜单管理","系统设置"]
  })
})

router.get('/oldNum', async (ctx, next) => {
  ctx.body = {
    data: 20000
  }
})

router.get('/string', async (ctx, next) => {
  console.log("string");
  ctx.body = {
    code: 200,
    str: 'koa2 string'
  }
})

router.post('/login', async (ctx, next) => {
  console.log(ctx.request.body);
  console.log(ctx.request.files);
  console.log("string");
  ctx.body = {
    str: '登录成功',
    token: 123456,
    data: ctx.request.body
  }
})

router.get('/json', async (ctx, next) => {
  let list = ['音乐1', '旅游2', '运动3', '历史4', '卡牌5']
  ctx.body = {
    title: "分类列表",
    data: list,
    code: 200,
    msg: "请求成功"
  }
})

router.post('/file', async (ctx, next) => {
  let list = ['音乐1', '旅游2', '运动3', '历史4', '卡牌5'];

  ctx.body = {
    title: "分类列表",
    data: list,
    code: 200,
    msg: "请求成功"
  }
})

router.post('/webupload', async (ctx, next) => {

  let filePath = ctx.request.files.file.filepath;

  let type = ctx.request.files.file.originalFilename.split(".");
  const filepath = path.resolve(path.join(__dirname + "/../public/web/"), `${new Date().getTime()}.${type[type.length - 1]}`);

  const readStream = fs.createReadStream(filePath);
  const writeStream = fs.createWriteStream(filepath);

  readStream.pipe(writeStream);

  let bool = true;
  readStream.on('end', function () {
    bool = true;
  })

  readStream.on('err', function () {
    bool = false;
  })
  ctx.body = {
    title: "web上传图片",
    code: bool ? 200 : 500,
    msg: bool ? "上传成功" : "上传失败"
  }

})

router.post('/wangEditor/webupload', async (ctx, next) => {

  let filePath = ctx.request.files.file.filepath;

  let type = ctx.request.files.file.originalFilename.split(".");
  let name = `${new Date().getTime()}.${type[type.length - 1]}`;
  const filepath = path.resolve(path.join(__dirname + "/../public/web/"), name);

  const readStream = fs.createReadStream(filePath);
  const writeStream = fs.createWriteStream(filepath);

  readStream.pipe(writeStream);

  let bool = true;
  readStream.on('end', function () {
    bool = true;
  })

  readStream.on('err', function () {
    bool = false;
  })

  ctx.body = {
    "errno": 0, // 注意：值是数字，不能是字符串
    "data": {
      "url": "http://192.168.1.3:3001/web/" + name, // 图片 src ，必须
      "alt": name, // 图片描述文字，非必须
      "href": "http://192.168.1.3:3001/web/" + name // 图片的链接，非必须
    }
  }

})


router.post('/uniupload', async (ctx, next) => {
  //uview 的上传方式
  let filePath = ctx.request.files.file.filepath;
  let time = new Date().getTime();
  let type = ctx.request.files.file.originalFilename.split(".");

  fs.writeFileSync(path.join(__dirname + "/../public/app/" + `${time}.${type[type.length - 1]}`), fs.readFileSync(filePath))

  ctx.body = {
    code: 200,
    msg: "上传成功",
    data: time + type
  }
})

module.exports = router